﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace CodeCrawler.UI.Functionalities.VisualStudio {
    
    /// <summary>
    /// Visual Studio.NET Integration
    ///     
    /// NOTE: This will only works with Visual Studio 2005 and Visual Studio 2008 ::: ONLY :::
    ///
    /// Code Crawler C# Project File Reader.
    /// Given a csjpro file will return a List collection containing all the source code files within the project
    /// 
    /// 
    ///             VisualStudioParser core = new VisualStudioParser(folderPath, projectFile);
    ///        List<string> allFiles = core.OpenProjectRetrieveFiles();
    /// 
    ///            if (core.FileCOunt > 0) {
    ///                allFiles.ForEach(new Action<string>(
    ///                                    delegate (string singleFile)
    ///                                    {
    ///                                        Console.WriteLine(singleFile);
    ///                                    }));
    ///                    
    ///            }
    /// </summary>
    public class CSPROJParser {

        private int _fileCount;
        private string _projectFile = string.Empty;


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="containingFolder">Application Folder ContainingFolder</param>
        /// <param name="csProjFileName">C# ProjectFileName (eg. CodeCrawler.csproj)</param>
        internal CSPROJParser(string containingFolder, String csProjFileName) {

            ProjectFile = csProjFileName;
            ApplicationFolder = containingFolder;
        }


        /// <summary>
        /// [S] .CSPROJ file name
        /// </summary>
        public string ProjectFile {
            set { _projectFile = value; }
        }
        /// <summary>
        /// [S] Project Container Folder
        /// </summary>
        public string ApplicationFolder
        {
            get; set;
        }
        /// <summary>
        /// [G] Numbers of file within the Application
        /// </summary>
        public int FileCOunt {
            get { return _fileCount; }
        }


        /// <summary>
        /// Open a specificated Project
        /// </summary>
        /// <returns></returns>
        internal List<string> OpenProjectRetrieveFiles() {

            if (!string.IsNullOrEmpty(_projectFile)) {
                var allSourceCodeFiles = new List<string>();
                
                var visualStudioProject = new FileInfo(string.Format("{0}{1}",ApplicationFolder, _projectFile));

                if (visualStudioProject.Exists) {
                    var visualStudioProjectFileReader = new XmlTextReader(visualStudioProject.FullName);
                    while (visualStudioProjectFileReader.Read()) {

                        XmlNodeType nType = visualStudioProjectFileReader.NodeType;

                        if (nType == XmlNodeType.Element) {
                            if (visualStudioProjectFileReader.Name.ToLower().IndexOf("compile") != -1) {
                                _fileCount++;
                                // Source Code File Path found
                                allSourceCodeFiles.Add(visualStudioProjectFileReader.GetAttribute(0));
                            }
                        }
                    }

                    return allSourceCodeFiles;
                }
                return null;
            }
            return null;
        }
    }
}